import java.util.Stack;

public class demo1 {
    public int calculate(String s) {
        char temp='+';
        Stack<Integer>stack=new Stack<>();
        char []arr=s.toCharArray();
        int i=0;
        int n=s.length();
        while(i<n){
            if(arr[i]==' ')i++;
            else if(arr[i]>='0'&&arr[i]<='9'){
                int sum=0;
                while (arr[i]>='0'&&arr[i]<='9'){
                    sum=sum*10+arr[i]-'0';
                    i++;
                }
                if(temp=='+')stack.add(sum);
                else if(temp=='-')stack.add(-sum);
                else if(temp=='*')stack.add(stack.pop()*sum);
                else stack.add(stack.pop()/sum);
            }
           else{
               temp=arr[i];
               i++;
            }
        }
        int ret=0;
        for (Integer x:stack){
            ret+=x;
        }
        return ret;
    }
}
